<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Follows extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();

        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $rows = $data['listrow'] : $rows = 20;
        $res = \db('home_news')
            ->alias('N')
            ->join('home_users U','N.fromid=U.id')
            ->join('home_users S','N.toid=S.id')
            ->where('N.toid',$data['id'])
            ->field('N.id,N.fromid,U.nickname fromname,N.toid,
            U.nickname toname,N.sqid,N.create_time,N.news_content,N.status')
            ->paginate($rows,true,['page'=>$page]);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function operateFollow(Request $request)
    {
        $data = $request->param();
        if (empty($data['id']) || empty($data['toid'])){
            return apistatus(1,'缺少请求数据');
        }
        self::checktoken($data['id']);
        $exist = \db('home_follow')->where(['uid'=>$data['id'],'followdid'=>$data['toid']])->find();
        if ($exist){
            $res = \db('home_follow')->where('id',$exist['id'])->delete();
            $re['status'] = 0;
        }else{
            $insert['uid'] = $data['id'];
            $insert['followdid'] = $data['toid'];
            $insert['create_time'] = time();
            $res = \db('home_follow')->insert($insert);
            $re['status'] = 1;
        }
        if ($res){
            $re['count'] = \db('home_follow')->where('followdid',$data['toid'])->count();
            return apistatus(1,'操作成功',$re);
        }else{
            return apistatus(1,'操作失败');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function cancleFollow(Request $request)
    {
        $data = $request->param();


    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    

}
